import pygame, random, sys
from pygame.locals import *

#import classes
#general idea -- the Game class draws Maps & Encounters, Maps are a list of 2d lists
#drawn from top to bottom with the player sprite moving around/interacting
#with them. Encounters are actual battles with everything labeled & set up
#in the style of older Pokemon battles


class Game:
    def __init__(self):
        self.loadContent()
        pygame.init()
        self.mainSurface = pygame.display.set_mode(self.screenres)
        pygame.display.set_caption(self.settings["TITLE"])
        self.mainLoop()
    
    def loadContent(self):
        try:
            #load the settings
            settings = open("settings.cfg", "r")
            content = settings.read()
            settings.close()
            content = content.split("\n")
            keys = []
            values = []
            
            for item in content:
                item = item.split(":")
                keys.append(item[0])
                values.append(item[1])
                
            self.settings = dict(zip(keys, values))
            self.applySettings()
            
           
        except Exception:
            settings.close()
            print "Error loading settings"
            
        #load images and sprites
        #load sounds
        #load whatever extra content is necessary

    def applySettings(self):
        #This method runs at the beginning of loadContent
        #NOTHING in here can depend on content being loaded
        #instead, content loading should depend on settings loaded here
        res = self.settings["SCREENRES"].split(",")
        self.width = int(res[0])
        self.height = int(res[1])
        self.screenres = (self.width, self.height)

    def mainLoop(self):
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()

game = Game()
